'use strict';
exports.main = async (event, context) => {
	//event为客户端上传的参数

	console.log('event : ', event)
	console.log('event : ', event.body)
	var db = uniCloud.database();
	var collection = db.collection('user');
	if (event.httpMethod == 'POST') {
		var res = event.body
		var obj = JSON.parse(res)
		let resx = {}
		if (obj.action == 'add') {
			// 格式化日对象
			let token = parseInt(Math.random() * 1000) + "@" + obj.pwd + "@" + obj.name + "@" + parseInt(Math
				.random() * 2000) //token规则
			resx = await collection.add({
				name: obj.name,
				pwd: obj.pwd,
				// src: obj.src,
				// baseCode: obj.baseCode,
				// baseCoder: obj.baseCoder,
				cid:obj.cid,
				formation:obj.formation
			})
			return {
				code: 200,
				token: token,
				data: resx,
			}
		} else if (obj.action == 'put') {
			if (obj.form.src) {
				resx = await collection.where({
					_id: obj.form._id
				}).update({
					src: obj.form.src
				})
			} else {
				resx = await collection.where({
					_id: obj.form._id
				}).update({
					name: obj.form.name
				})
			}
			return {
				code: 200,
				data: resx
			}
		} else if (obj.action == 'delete') {
			resx = await collection.doc(obj._id).remove()
			return {
				code: 200,
				data: resx
			}
		} else if (obj.action == 'get') {
			resx = await collection.get()
			return {
				code: 200,
				data: resx
			}
		} else if (obj.action == 'getOne') {
			resx = await collection.where({
				_id: obj._id
			}).get()
			return {
				code: 200,
				data: resx
			}
		} else {
			return {
				code: 200,
				data: obj.action
			}
		}

	} else {
		return {
			code: 200,
			data: "不是post请求"
		}
	}

}
